package amazon

import (
	"encoding/json"
	"errors"
	"fmt"
	"gitee.com/wengo/go-mamba"
	"github.com/PuerkitoBio/goquery"
	"github.com/tidwall/gjson"
	"net/url"
	"regexp"
	"strings"
)

type Account struct {
	amz *Amazon
}

func NewAccount(amazon *Amazon) *Account {
	return &Account{
		amz: amazon,
	}
}

func (a *Amazon) AcceptAllCookies(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	csrftoken, exists := document.Find("input[name='anti-csrftoken-a2z']").Attr("value")
	if exists {
		resp, newDocument, err = a.Submit("/cookieprefs?ref_=portal_banner_cpp", POST, nil, nil, map[string]string{
			"mdl":                       "false",
			"amazon-ads-systems-switch": "on",
			"ADVERTISING":               "on",
			"third-parties-switch":      "on",
			"accept":                    "all",
			"anti-csrftoken-a2z":        csrftoken,
		})

		return resp, newDocument, err
	}

	return nil, nil, fmt.Errorf("无法找到cookies页面的csrftoken")
}

func (a *Amazon) CheckHasCookiePreferences(document *goquery.Document) bool {
	return document.Find("#sp-cc").Length() > 0
}

func (a *Amazon) SetCookiePreferences(document *goquery.Document) (err error) {
	action, method, formData, err := a.ExtractFormData(document, "#sp-cc")
	if err != nil {
		return err
	}

	_, _, err = a.Submit(action, method, nil, nil, formData)

	return nil
}

func (a *Amazon) ExtractRegisterUrl(document *goquery.Document) (string, error) {
	html, err := document.Html()
	if err != nil {
		return "", err
	}

	compile := regexp.MustCompile(`<a href='(https://[^/]+?/ap/register[^']+)'`)
	result := compile.FindAllStringSubmatch(html, -1)
	if len(result) > 0 {
		return result[0][1], nil
	}

	return "", fmt.Errorf("无法提取注册链接")
}

func (a *Amazon) ExtractLoginUrl(document *goquery.Document) (string, error) {
	result, exits := document.Find("#nav-link-accountList").Attr("href")
	if exits {
		return result, nil
	}

	return "", fmt.Errorf("无法提取登陆链接")
}

func (a *Amazon) ExtractSecurityUrl(document *goquery.Document) (string, bool) {
	return document.Find(".ya-card-cell a[href*='/ap/cnep']").Attr("href")
}

// 注册
//
// email: 用户邮箱
//
// username: 用户名
//
// password: 用户密码
//
// document: 注册页面
func (a *Amazon) Register(name, email, password string, document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, registerFormData, err := a.ExtractFormData(document, "form[name='register']")
	if err != nil {
		return nil, nil, errors.New(fmt.Sprintf("注册失败: %s", err.Error()))
	}

	registerFormData["customerName"] = name
	registerFormData["email"] = email
	registerFormData["password"] = password
	registerFormData["passwordCheck"] = password
	//loginFormData["metadata1"] = `ECdITeCs:+UvNK7U6OpYJmcbk2pE0JJD5wHU7Rfu1vXp+Apnh+mF6n3Ug7ljxHXXXd5BJ89N6N9AO5fYryUP/W73mPeV+9jgg0lNkTJiaQEmDDrtPtIJdYcJNll0YBvSdZWR9CjvMxsvxZedjh2Lfyf6s5Jb1OHLdAH/PqWRioHZywbomvYd0nBgmV2PVS+jGKS6P9TnpDcexFlRJT6wNUwFK19uhrh2OQpfAHj6cLXHcGl/1AA5QT3VSazKYaWHR6KVwKnJSoPFlwvbPTz5lXnmOB7yg3Y7zjkS82mWSja/o7rzxwZpZQ+QGiqdPPy9BVhIwMy/W0f7T406TEzyCBLy9b8aYhSYOanjz8SWRoojXxEG9jV2IDV4iP/5Qh2/cfZxVaejJe71kXeSzW3N55ZP7SY/Xz8TFmMLaimvtqpZMY9ngW52KMFvJWBemdMGq9LcZGO+GUnGARnbnnQ9ALYzg+L9RCMFEsWAl1Lf4R2s59zcUN6RFg8XJ7vuFfZChHKu+Um/PoP2AAgTdJE8dXu7dSrTKJhjoG+KrHShxr+DD194eh8USIjWcrLmcBk7BZqfAe+w099CDxM5IMgd9PurmAs/jzAKMgO+76MBSwnxgYD08LAkhtRm1usWfg+Kfb8ljEGHux25fF+6T504LgA1BbHBHjFPqNT6Ur5JtpQCUKdmmNSEpljnApPLwT8WCjojjjOl3btirwMvXJCM5S1usp3dykT/mYOuaHWjPFCB67Q4SDNF9NurAhwhXKwubo6Jka0I/DkHw0wetFqnFXSIyrE6cpeFqa9DW9sMN2x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Ubq1JIMEUv8EmH4aT4QxkYGZV98d0xjnc0mM5CQdvSsM39IcfQsjOlmKz9MykiB+z8gAk0txsjSmq8dwS/WOwG4ZUcC+ZHpDc9FacsKBEx7QlbAf3jjNYnupLb6aEQ2eokwq5Nx3HIWeGRAnvwfCrFnXEGKNBMzP90XzCK3nBlid31CWoJK8gxf0iArh295WfTYijjWi3iUkGy7zGvvBkdvO8xNmGciFuyUt6eXnRUzZup9RE31u4+JhWLccUaRk0sF9jRjx5f/SNsJ44jniIrEQrk/tGhlTlXfZe10JLUARFwBYX4LSvWre0Uh10rAQEqKYh6aUfyKYBW6INnTRr6RRFwnbpzpANsBMsRi1Y6TOByf/wr9KPisRYP4BDN9wi0LS7IkkVXp//E/I25GMx5l/mjMPjLL3yvdcqdmFmNSHgECsVyvS9zd5Mh8+3MRHWeBeOT47n/OpB+HrRHD3p4guEdG0NLi4nxE0BntJdsWtsp8pESUP0L0tbTSFaBy6zVhFLZCVaOy78YkksDXtA3cYGfFiM0qx+5fpUIv4gwEuLWUWZk+PQLRIiNdlIYB2LCmISNtvwzGGnU8f8nB/MAORTjWmNamkCb0N++jDR2U4KgKYY9Nl4XEnnVvQ8Wj/428BY5SFT5vkwqDDElgAzjjkFMG6o3sDlDAOTscQx2HfEzK6xBVuj6NpTSXMjeILy60BkH+/NRtHl+WFxEXdtgLjnREEnR2iga4jVTvF9937FVk8v/HFXmalLz6NJp7c7bDV8oKQNJ5hjlSTtXj0PaJG6SlRJvU6blTauz4w5DbZ/K4nzmI1sK5UX8d/PdNsqaipU3yL1oMFLhS2Fe9TvQUvdsyP4EzI1sa2zk1KDA1oiGwTfxH2GXZWBMyIcznjfhxgiquYHH2neBNrK6q7bd4NtejtV/n2CicsAoTZ4=`
	resp, newDocument, err = a.Submit(action, method, nil, nil, registerFormData)
	return
}

func (a *Amazon) IsFunCaptcha(document *goquery.Document) bool {
	return document.Find("#cvf-arkose-frame").Length() > 0
}

func (a *Amazon) IsNormalCaptcha(document *goquery.Document) bool {
	return document.Find(".cvf-captcha-img").Length() > 0
}

func (a *Amazon) ExtractCaptchaImage(document *goquery.Document) (captchaUrl string, err error) {
	captchaUrl, exists := document.Find(".cvf-captcha-img img").Attr("src")
	if exists {
		return captchaUrl, nil
	}

	return "", fmt.Errorf("无法抓取到验证码链接")
}

// 提取验证码
//
// document: 注册页面
//func (a *Amazon) ExtractCaptchaImage(document *goquery.Document) (b64image string, rawImage []byte, imageType string, err error) {
//	img, exists := document.Find(".cvf-captcha-img img").Attr("src"); if exists {
//		return global.BM_API.DownloadCaptchaImage(img)
//	}
//	return
//}

// 提交验证码
//
// document: 注册页面
func (a *Amazon) SubmitFunCaptchaVerify(document *goquery.Document, recognition string) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, "#cvf-arkose-captcha-form")
	if err != nil {
		return nil, nil, err
	}

	formData["cvf_captcha_arkose_response_token"] = recognition

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) SubmitNormalCaptchaVerify(document *goquery.Document, recognition string) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, ".cvf-widget-form-captcha")
	if err != nil {
		return nil, nil, err
	}

	formData["cvf_captcha_input"] = recognition

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

// 检查验证码是否正确
//
// document: 验证码页面
func (a *Amazon) CaptchaVerifyResult(document *goquery.Document) bool {
	return document.Find("#cvf-input-code").Length() > 0
}

func (a *Amazon) IsEmailOptPage(document *goquery.Document) bool {
	return document.Find("#verification-code-form").Length() > 0
}

func (a *Amazon) IsMobilePage(document *goquery.Document) bool {
	return document.Find("#mobileClaimDisclosureMessage").Length() > 0
}

func (a *Amazon) IsMobileOptPage(document *goquery.Document) bool {
	return document.Find("#cvf-resend-code-section").Length() > 0
}

func (a *Amazon) SubmitEmailOTP(document *goquery.Document, code string) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, "#verification-code-form")
	if err != nil {
		return nil, nil, err
	}

	formData["code"] = code

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) SubmitResendEmailOTP(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, "#verification-code-form")
	if err != nil {
		return nil, nil, err
	}

	formData["action"] = "resend"
	delete(formData, "autoReadHiddenInputCode")
	delete(formData, "autoReadStatus")
	delete(formData, "code")
	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) SubmitPhone(document *goquery.Document, cc, phone string) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, ".cvf-widget-form")
	if err != nil {
		return nil, nil, err
	}

	formData["cvf_phone_cc"] = cc
	formData["cvf_phone_num"] = phone

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) CheckPhone(document *goquery.Document) (err error) {
	if document.Find(".cvf-widget-alert-message").Length() > 0 {
		return fmt.Errorf("%s", document.Find(".cvf-widget-alert-message").Text())
	}

	return nil
}

func (a *Amazon) SubmitMobileOTP(document *goquery.Document, code string) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, ".cvf-widget-form")
	if err != nil {
		return nil, nil, err
	}

	delete(formData, "cvf_action_#radio2#")
	delete(formData, "cvf_action_#radio3#")
	formData["code"] = code

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) SubmitResendMobileOTP(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, ".cvf-widget-form")
	if err != nil {
		return nil, nil, err
	}

	delete(formData, "cvf_action_#radio2#")
	delete(formData, "cvf_action_#radio3#")
	formData["cvf_action"] = "resend"

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) ChangeMobile(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	_, method, formData, err := a.ExtractFormData(document, ".cvf-widget-form")
	if err != nil {
		return nil, nil, err
	}

	delete(formData, "cvf_action_#radio2#")
	delete(formData, "cvf_action_#radio3#")
	formData["cvf_action"] = "collect"

	resp, newDocument, err = a.Submit(`/ap/cvf/verify`, method, nil, nil, formData)

	return
}

func (a *Amazon) UnbindMobile(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, formData, err := a.ExtractFormData(document, "#cnep_1a_mobile_phone_form")
	if err != nil {
		return nil, nil, err
	}

	resp, newDocument, err = a.Submit(action, method, nil, formData, nil)

	return
}

func (a *Amazon) ForceEmailRegister(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	link, exists := document.Find("a[href*='/ap/register/ref=ap_register_mase_warn_claim_pop']").Attr("href")
	if exists {
		resp, newDocument, err = a.Navigate(link)
	} else {
		err = fmt.Errorf("没有找到强制邮箱注册的链接")
	}

	return
}

func (a *Amazon) ForceRegister(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, formData, err := a.ExtractFormData(document, "#ap_account_conflict_warning_customer_actions")
	if err != nil {
		return nil, nil, err
	}

	resp, newDocument, err = a.Submit(action, method, nil, formData, nil)

	return
}

func (a *Amazon) DeleteMobile(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, formData, err := a.ExtractFormData(document, "#auth-remove-phone-claim")
	if err != nil {
		return nil, nil, err
	}

	resp, newDocument, err = a.Submit(action, method, nil, nil, formData)

	return
}

func (a *Amazon) IsSwitchAccount(document *goquery.Document) bool {
	return document.Find("#ap-account-switcher-container").Length() > 0
}

func (a *Amazon) SwitchAccount(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	arbtoken, exists := document.Find("#ap-account-switcher-container").Attr("data-arbtoken")
	if exists {
		resp, document, err = a.Navigate(fmt.Sprintf(`/ap/cvf/request.embed?arb=%s&CVFVersion=0.1.0.0-2022-03-24&AUIVersion=3.21.8-2022-03-24`, arbtoken))
		action, method, formData, err := a.ExtractFormData(document, "form[action*='switchaccount']")
		if err != nil {
			return nil, nil, err
		}

		accountId, _ := document.Find("a[data-name='switch_account_request']").Attr("data-value")
		formData["switch_account_request"] = accountId
		resp, document, err = a.Submit(action, method, nil, nil, formData)
		redirectUrl := gjson.Get(resp.String(), "redirectUrl").String()
		if redirectUrl != "" {
			return a.Navigate(redirectUrl)
		}

		return nil, nil, fmt.Errorf("无法获取切换账户的跳转链接")
	}

	return
}

// 登录
//
// email: 用户邮箱
//
// username: 用户名
//
// password: 用户密码
//
// document: 登录页面
func (a *Amazon) Login(email, password string, document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, loginFormData, err := a.ExtractFormData(document, "form[name='signIn']")
	if err != nil {
		return nil, nil, errors.New(fmt.Sprintf("登录失败: %s, 可能因为账户无法登陆导致", err.Error()))
	}

	//loginFormData["appAction"] = "SIGNIN_PWD_COLLECT"
	loginFormData["email"] = email
	loginFormData["password"] = password
	//loginFormData["metadata1"] = `ECdITeCs:2+B+xaYO53D5L9wwQe9sf9ig83VqCcPV3tuzUmhr211aXdWjHL1/vKq6Kdt1L4n5oxjAkuugITY7Vic9I7nRH1zkEDD5aQniMZujmge5Mq1cdiTm7+FBdPbyb8yik5fZOZCsGhPbKqXhOy+dpkDylaW5LK7pKEC1i8AhNewl3dfejRGxL/d2zbwf7SDMYvrvFU+8M2CvMYa2BNTYrioZxdTyUKPf908vEPMyV+AEvcqrsQOeKL+ToDRUmi2dCWRF97ENHK+1YHXHRM80HTQWUKNrae+nM6Mp99M+rop8yLBqnlKT/qSqpgieGZpaNHTRLqQohzecjWZjnTAx6uDWoXMyT/c/KoWx+fIYg/G+SgYvV7fX4oAkbCHiFqR/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1rwUI6/wTOYaAoxW3sDK1cFCNUU4gPkAFamgfQXOXxJYAXxpnT81HBBIdG/XkWC6X7FVQcNvCyc0+3L4dUak4ms65y599yCnBSXJ7j8Zq5x363T2zRZ4TsuDmmK59DddjpGf6xg3kxD3CmwFDzCqlnCWsRxIGw5B91wcz0dEwyGRMHZhLy6EgTkEIxQ41EY9vz16gELw0l3crPF8nwoBSLF3DqQXPPHm+AmE9ya7g0gZ7fKN6FBVWj+3+DMFf5ebSF/zNENd+k+PDcVn9AhLlUSveCsJgg3AO5SeBSauTi35A5F8IUVCckoXcsiD1q69+Td4Z96P7pmdjGId8BlOLWHsEDJ34vvqxnsItqzdJYzI5kVhEXqJElK81gp9311HZXuI45lj/lGjn/WSEdddP69hBXEKYy7W5CaQwGVQm/PjITRr51Y7a7UlICBHxS1SjC5F1EpBRrRyXlOo/iemEq+L/Ilx4+MjyfMgVgZ8js7ol1`
	//map[string]string{"Content-Length": "", "Origin": "https://www.amazon.com"}
	resp, newDocument, err = a.Submit(action, method, nil, nil, loginFormData)
	//if !a.CheckIsLogin(resp) {
	//	return nil, nil, errors.New("账户被封")
	//}

	return
}

func (a *Amazon) InputEmail(email, metadata1 string, document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, loginFormData, err := a.ExtractFormData(document, "form[name='signIn']")
	if err != nil {
		return nil, nil, errors.New(fmt.Sprintf("登录失败: %s, 可能因为账户无法登陆导致", err.Error()))
	}

	//loginFormData["appAction"] = "SIGNIN_PWD_COLLECT"
	loginFormData["email"] = email

	loginFormData["metadata1"] = metadata1
	//loginFormData["metadata1"] = `ECdITeCs:2+B+xaYO53D5L9wwQe9sf9ig83VqCcPV3tuzUmhr211aXdWjHL1/vKq6Kdt1L4n5oxjAkuugITY7Vic9I7nRH1zkEDD5aQniMZujmge5Mq1cdiTm7+FBdPbyb8yik5fZOZCsGhPbKqXhOy+dpkDylaW5LK7pKEC1i8AhNewl3dfejRGxL/d2zbwf7SDMYvrvFU+8M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4tfBUUQieKy/zs2ZtJEh3efxlPAw6a9i7Cqq4kcRcsTKgYlyvMVEdb8bCGgZ4SX7v6EbpuEvtDypaofvtXivQxNJu5+lt2x/E60H+h6cBUia4/RxxH9whweeeSFVgkR/N0mJ8XKJO5ufP99NY3ewW+c/+caP/+qAcVFIBBhWPqZcNFVHwJmF1rwUI6/wTOYaAoxW3sDK1cFCNUU4gPkAFamgfQXOXxJYAXxpnT81HBBIdG/XkWC6X7FVQcNvCyc0+3L4dUak4ms65y599yCnBSXJ7j8Zq5x363T2zRZ4TsuDmmK59DddjpGf6xg3kxD3CmwFDzCqlnCWsRxIGw5B91wcz0dEwyGRMHZhLy6EgTkEIxQ41EY9vz16gELw0l3crPF8nwoBSLF3DqQXPPHm+AmE9ya7g0gZ7fKN6FBVWj+3+DMFf5ebSF/zNENd+k+PDcVn9AhLlUSveCsJgg3AO5SeBSauTi35A5F8IUVCckoXcsiD1q69+Td4Z96P7pmdjGId8BlOLWHsEDJ34vvqxnsItqzdJYzI5kVhEXqJElK81gp9311HZXuI45lj/lGjn/WSEdddP69hBXEKYy7W5CaQwGVQm/PjITRr51Y7a7UlICBHxS1SjC5F1EpBRrRyXlOo/iemEq+L/Ilx4+MjyfMgVgZ8js7ol1`
	//map[string]string{"Content-Length": "", "Origin": "https://www.amazon.com"}
	resp, newDocument, err = a.Submit(action, method, nil, nil, loginFormData)
	//if !a.CheckIsLogin(resp) {
	//	return nil, nil, errors.New("账户被封")
	//}

	return
}

func (a *Amazon) InputPassword(password, metadata1 string, document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, loginFormData, err := a.ExtractFormData(document, "form[name='signIn']")
	if err != nil {
		return nil, nil, errors.New(fmt.Sprintf("登录失败: %s, 可能因为账户无法登陆导致", err.Error()))
	}

	//loginFormData["appAction"] = "SIGNIN_PWD_COLLECT"
	loginFormData["password"] = password
	loginFormData["metadata1"] = metadata1
	delete(loginFormData, "email_#radio2#")
	//loginFormData["metadata1"] = `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lpWip8LqBOGdhcG4VEuNJRmpRd2ATlQ9pkAs22NTuIXprGX1EcvE1m1IYkE541m9OGBUZrv5/n3BuX0B5kuynYz/2lPJtVEf3/XZAJyku0D59CferdXf/e9iS3BIfsD6cHAEqXQE+8W2pzKqDJFMKCHBnwUhtRjJQyX3mW0Sc4b6O4TxI+ObqVjoLLDFzKnNocTx3VOD0O4tfBUUQieKy/zs2ZtJEh3efxlPAw6a9i7Cqq4kcRcsTKgYlyvMVEdb8bCGgZ4SX7v6EbpuEvtDypaofvtXivQxNJu5+lt2x/E60H+h6cBUia4/RxxH9whweeeSFVgkR/N0mJ8XKJO5ufP99NY3ewW+c/+caP/+qAcVFIBBhWPqZcNFVHwJmF1rwUI6/wTOYaAoxW3sDK1cFCNUU4gPkAFamgfQXOXxJYAXxpnT81HBBIdG/XkWC6X7FVQcNvCyc0+3L4dUak4ms65y599yCnBSXJ7j8Zq5x363T2zRZ4TsuDmmK59DddjpGf6xg3kxD3CmwFDzCqlnCWsRxIGw5B91wcz0dEwyGRMHZhLy6EgTkEIxQ41EY9vz16gELw0l3crPF8nwoBSLF3DqQXPPHm+AmE9ya7g0gZ7fKN6FBVWj+3+DMFf5ebSF/zNENd+k+PDcVn9AhLlUSveCsJgg3AO5SeBSauTi35A5F8IUVCckoXcsiD1q69+Td4Z96P7pmdjGId8BlOLWHsEDJ34vvqxnsItqzdJYzI5kVhEXqJElK81gp9311HZXuI45lj/lGjn/WSEdddP69hBXEKYy7W5CaQwGVQm/PjITRr51Y7a7UlICBHxS1SjC5F1EpBRrRyXlOo/iemEq+L/Ilx4+MjyfMgVgZ8js7ol1`
	//map[string]string{"Content-Length": "", "Origin": "https://www.amazon.com"}
	resp, newDocument, err = a.Submit(action, method, nil, nil, loginFormData)
	//if !a.CheckIsLogin(resp) {
	//	return nil, nil, errors.New("账户被封")
	//}

	return
}

func (a *Amazon) CheckLoginError(document *goquery.Document) string {
	return document.Find("#auth-error-message-box span.a-list-item").Text()
}

func (a *Amazon) CheckLoginInput(document *goquery.Document) string {
	if document.Find("#ap-credential-autofill-hint[name='email']").Length() > 0 {
		return "password"
	} else {
		return "email"
	}
}

func (a *Amazon) CheckIsLogin(resp *mamba.Response) bool {
	if strings.Contains(resp.RawResponse.Request.URL.String(), "fixup") {
		return true
	}

	cookies := resp.Cookies()

	for _, cookie := range cookies {
		if cookie.Name == "x-main" && cookie.Value != "" {
			return true
		}
	}

	return false
}

func (a *Amazon) GetCookies() (cookies []byte, err error) {
	u, _ := url.Parse(a.client.BaseURL)
	//u, _ := url.Parse(a.client.BaseURL)
	c := a.client.GetClient().Jar.Cookies(u)

	for _, cookie := range c {
		fmt.Println(cookie.Name, cookie.Value, cookie.Expires, cookie.Domain, cookie.Path)
	}

	cookies, err = json.Marshal(a.client.GetClient().Jar.Cookies(u))

	fmt.Println(string(cookies))
	return
}

// 账户修复
//
// document: 登录之后跳转的document
func (a *Amazon) AccountFixup(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	link, _ := document.Find("#ap-account-fixup-phone-skip-link").Attr("href")
	resp, newDocument, err = a.Navigate(link)
	return
}

// 检查是否需要登录
//
// url: 当前页面的链接

func (a *Amazon) IsNeedLogin(url string) bool {
	if strings.Contains(url, "signin") {
		return true
	}
	return false
}

// IsLogined 检查是否已经登陆
//
// url: 当前页面的链接
func (a *Amazon) IsLogined(body string) bool {
	return !strings.Contains(body, "GLUXSignInButton")
}

// 检查是否需要账户修复
//
// url: 当前页面的链接
func (a *Amazon) IsNeedAccountFixup(url string) bool {
	if strings.Contains(url, "accountfixup") {
		return true
	}

	return false
}

// 提取订单列表的订单号
//
// url: 当前页面的链接
func (a *Amazon) ExtractOrderId(document *goquery.Document) string {
	if document.Find("bdi").Length() > 0 {
		return document.Find("bdi").Eq(0).Text()
	}

	return ""
	//link, _ := document.Find("a[id^='Cancel-items']").Attr("href")
	//u, err := url.Parse(link)
	//if err != nil {
	//	panic(err)
	//}
	//
	//query, _ := url.ParseQuery(u.RawQuery)
	//return query.Get("orderID")
}

// 取消订单
//
// url: 当前页面的链接
func (a *Amazon) CancelOrder(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	if document.Find("a[href*='useCase=cancel']").Length() > 0 {
		link, exists := document.Find("a[href*='useCase=cancel']").Attr("href")
		if exists {
			resp, newDocument, err = a.Navigate(link)
		}
	}

	return
}

// 取消订单
//
// url: 当前页面的链接
func (a *Amazon) ConfirmCancelOrder(document *goquery.Document) (resp *mamba.Response, newDocument *goquery.Document, err error) {
	action, method, formData, err := a.ExtractFormData(document, "form[action*='/gp/css/order/edit.html']")
	if err != nil {
		return nil, nil, err
	}
	for name, _ := range formData {
		if strings.Contains(name, "cq.cancelItem") {
			formData[name] = "on"
		}
	}

	delete(formData, "cq.submit")

	formData["cancel.reason"] = "other"

	resp, newDocument, err = a.Submit(action, method, nil, nil, formData)
	return
}
